var port = (process.env.VMC_APP_PORT || 3000);
var host = (process.env.VCAP_APP_HOST || 'localhost');
var http = require('http');

/*
http.createServer(function (req, res) {
    var html = '<html>'  
        +'<head>'  
        +'<title>nodejs</title>'  
        +'</head>'  
        +'<body>'  
        +'<a href="itms-services://?action=download-manifest&url=https://git.oschina.net/sensesw/SRAM/raw/master/TEST.plist">TEST</a>'
        +'</body>'  
        +'</html>';  
  res.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'});
  res.write(html);
  res.end();
}).listen(port, host);
*/

/*
    var express = require('express.io');
    var app = express().http().io();

    app.use(express.cookieParser());
    app.use(express.session({secret: 'monkey'}));

    app.use(express.static(__dirname + 'web'));

    app.listen(port);
*/

var mime = require("mime"),path = require("path"),fs = require("fs");
http.createServer(function (req, res) {
	if (req.url.charAt(req.url.length - 1) == '/') {
		req.url += "index.html";
    }

    var targetPath = path.normalize(__dirname + req.url);
    
//     console.log("path:"+targetPath);
	
	var stats = fs.stat(targetPath);
	
	fs.stat(targetPath, function (err, stats) {
        if (err) {
            console.log(err);
            return; // exit here since stats will be undefined
        }

        if (stats.isFile()) {
			res.statusCode = 200;
			res.setHeader('Content-type', mime.lookup(targetPath));
			fs.createReadStream(targetPath).pipe(res);
        }
        if (stats.isDirectory()) {
			res.statusCode = 404;
			res.end('404 Not Found');
        }
    });	
}).listen(port,host);

/*

10-4-2-20
67696947200
53291696128
Linux
{ 
node: '0.4.12',
v8: '3.1.8.26',
ares: '1.7.4',
ev: '4.4',
openssl: '0.9.8k' }
*/

var os = require('os');
console.log(os.hostname());
console.log(os.totalmem());
console.log(os.freemem());
// console.log(os.tmpdir());
// console.log(os.uptime());
console.log(os.type());
// console.log(os.platform());
// console.log(os.cpus());
console.log(process.versions);

/*
var exec = require('child_process').exec, child;

 child = exec('npm install express',
 function (error, stdout, stderr) {
     console.log('stdout: ' + stdout);
     console.log('stderr: ' + stderr);
     if (error !== null) {
          console.log('exec error: ' + error);
     }
 });
*/